<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\analysis\report;

use app\enum\CommonEnum;
use app\model\mongo\MongoReportModel;
use app\service\analysis\report\kuaishou\VideoReportService as KS;
use app\service\analysis\report\ocean\VideoReportService as OE;
use app\service\analysis\report\qq\VideoReportService as QQ;
use app\service\BaseService;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Model;

final class ReportVideoService extends BaseService
{
    /**
     * @var null|KS|OE|QQ|ReportService 日报服务服务
     */
    private ?ReportService $service;

    public function __construct(int $mediaId = 0)
    {
        if ($mediaId == CommonEnum::OE_MEDIA) {
            $this->service = new OE();
        }
        if ($mediaId == CommonEnum::KS_MEDIA) {
            $this->service = new KS();
        }
        if ($mediaId == CommonEnum::QQ_MEDIA) {
            $this->service = new QQ();
        }
    }

    /**
     * 是否是日报服务
     */
    public function isService(): bool
    {
        return $this->service instanceof ReportService || self::setError('获取日报服务失败');
    }

    /**
     * 获取待执行报表.
     * @param int $page 当前页
     * @param int $limit 分页数
     * @return array|Collection|MongoReportModel[]
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getWaitDayList(int $page = 1, int $limit = 200)
    {
        return $this->service::getList(['is_sync' => MongoReportModel::SYNC_NO], $page, $limit);
    }

    /**
     * 获取待修复报表.
     * @param int $page 当前页
     * @param int $limit 分页数
     * @return array|Collection|MongoReportModel[]
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getFixDayList(int $page = 1, int $limit = 200)
    {
        return $this->service::getList(['is_sync' => MongoReportModel::SYNC_FIX], $page, $limit);
    }

    /**
     * 待归档素材列表.
     * @param int $page 当前页
     * @param int $limit 分页数
     * @return array|Collection|MongoReportModel[]
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getWaitArchiveList(int $page = 1, int $limit = 200)
    {
        return $this->service::getList(['is_archive' => MongoReportModel::ARCHIVE_NO, 'is_sync' => MongoReportModel::SYNC_YES], $page, $limit);
    }

    /**
     * 账户+素材汇总.
     * @param array $where 请求参数
     */
    public function getAccountStat(array $where = []): array
    {
        return $this->service->getAccountStat($where);
    }

    /**
     * 通过id获取数据.
     *
     * @param string $objectId 主键id
     * @return array|mixed|Model|MongoReportModel
     */
    public function getModelById(string $objectId = '')
    {
        return $this->service::getModel()->where(['_id' => $objectId])->findOrEmpty();
    }

    /**
     * 获取MD5值
     */
    public function getSignature(MongoReportModel $model): string
    {
        return strval($this->service->getReportAttr($model, 'signature'));
    }

    /**
     * 获取系统素材值
     */
    public function getIsSystem(MongoReportModel $model): int
    {
        return intval($this->service->getReportAttr($model, 'is_system'));
    }
}
